package com.vf.admin.utils;

import com.vf.admin.client.common.Callback;
import lombok.SneakyThrows;


import java.util.concurrent.*;



public class ThreadUtil {

    public static final ScheduledThreadPoolExecutor mThreadPool = new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2, new ThreadPoolExecutor.CallerRunsPolicy());

	public static final ThreadPoolExecutor fixedExecutorService = new ThreadPoolExecutor(100,
			100,
			0L,
			TimeUnit.MILLISECONDS,
			new LinkedBlockingQueue<Runnable>(),
			Executors.defaultThreadFactory(),
			new ThreadPoolExecutor.CallerRunsPolicy());

	public static final ThreadPoolExecutor firendFixedExecutorService = new ThreadPoolExecutor(100,
			100,
			0L,
			TimeUnit.MILLISECONDS,
			new LinkedBlockingQueue<Runnable>(),
			Executors.defaultThreadFactory(),
			new ThreadPoolExecutor.CallerRunsPolicy());

	//投票
	public static final ExecutorService propoFixedExecutorService = new ThreadPoolExecutor(100,
			100,
			0L,
			TimeUnit.MILLISECONDS,
			new LinkedBlockingQueue<Runnable>(),
			Executors.defaultThreadFactory(),
			new ThreadPoolExecutor.CallerRunsPolicy());

	//xmpp
	public static final ExecutorService xmppFixedExecutorService = new ThreadPoolExecutor(100,
			100,
			0L,
			TimeUnit.MILLISECONDS,
			new LinkedBlockingQueue<Runnable>(),
			Executors.defaultThreadFactory(),
			new ThreadPoolExecutor.CallerRunsPolicy());

	/*public static final ThreadPoolExecutor mThreadPool = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors()*2,
			Runtime.getRuntime().availableProcessors()*2,
			0L,
			NANOSECONDS,
			new LinkedBlockingQueue<>(),
			Executors.defaultThreadFactory(),
			new ThreadPoolExecutor.CallerRunsPolicy());*/

//    public static final ScheduledExecutorService mThreadPool = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors() * 2);
    //	public static final ExecutorService executorService = Executors.newCachedThreadPool();
//    public static final ExecutorService fixedExecutorService = Executors.newFixedThreadPool(100);
//    public static final ExecutorService firendFixedExecutorService = Executors.newFixedThreadPool(100);
    //投票
//    public static final ExecutorService propoFixedExecutorService = Executors.newFixedThreadPool(20);
    //xmpp
//    public static final ExecutorService xmppFixedExecutorService = Executors.newFixedThreadPool(20);

    /**
     * @param @param callback    参数
     * @Description: TODO(立即执行 线程)
     */
    public static void executeInThread(Callback callback) {
        mThreadPool.execute(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                try {
                    callback.execute(Thread.currentThread().getName());
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        });
    }

    public static void executeInThread(Callback callback, Object obj) {
        mThreadPool.execute(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                callback.execute(obj);
            }
        });
    }

    /**
     * @param @param callback 延时 秒钟
     * @Description: TODO(延时执行线程)
     */
    public static void executeInThread(Callback callback, long delay) {
        mThreadPool.schedule(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                try {
                    callback.execute(Thread.currentThread().getName());
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        }, delay, TimeUnit.SECONDS);
    }

    /**
     * @param @param callback 延时 秒钟
     * @Description: TODO(延时执行线程)
     */
    public static void executeInCachedThread(Callback callback) {
        fixedExecutorService.execute(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                try {
                    callback.execute(Thread.currentThread().getName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
